/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package di.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Properties;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import sun.awt.AppContext;

/**
 *
 * @author Kilian Ito
 */
public class DIIO {
    protected static final Properties properties = new Properties();
    private static final Logger logger = Logger.getLogger(DIIO.class);
    
    public static void setConstantsFile(String path) {
        FileInputStream con = null;
        try {
            con = new FileInputStream(path);
            properties.load(con);
            con.close();
        } catch (IOException e) {
            logger.log(Level.ERROR, "Properties File not found", e);
        }
    }
    
    public static String getEncodingText(String text) {
        byte[] uniqueKey = text.getBytes();
        byte[] hash = null;
        StringBuilder hashString = new StringBuilder();
        try {
            String hashCode = getProperties("user.password.encode");
            logger.log(Level.DEBUG, "encoding in "+hashCode);
            hash = MessageDigest.getInstance(hashCode).digest();
            for (int i = 0; i < hash.length; i++) {
                String hex = Integer.toHexString(hash[i]);
                if (hex.length() == 1) {
                    hashString.append('0');
                    hashString.append(hex.charAt(hex.length() - 1));
                } else
                    hashString.append(hex.substring(hex.length() - 2));
            }
        } catch (NoSuchAlgorithmException e) {
            logger.log(Level.ERROR, "Algorithme Error", e);
        }
        return hashString.toString();
    }
    
    public static String getProperties(String key) {
        String property = properties.getProperty(key);
        if (property == null)
            logger.log(Level.ERROR, "Property not found");
        return property;
    }
}
